package com.websocket.websocketproject.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.websocket.websocketproject.config.Constants;
import com.websocket.websocketproject.websocket.ChatMessageHandler;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/notify")
public class NotifyController {
    @Resource
    ChatMessageHandler chatMessageHandler;

    @PostMapping("/update")
    public String update(@RequestBody List<JSONObject> list) {
        TextMessage message = new TextMessage(JSONArray.toJSONString(list));
        chatMessageHandler.sendMessageToUser(Constants.WEBSOCKET_USERNAME, message);
        return "success";
    }
}
